const mongoose = require('mongoose');

/**
 * 健康知识模型
 * 存储各类健康知识文章，包括糖尿病预防、健康运动、饮食推荐等
 */
const HealthKnowledgeSchema = new mongoose.Schema(
  {
    title: {
      type: String,
      required: [true, '标题不能为空'],
      trim: true,
      maxlength: [100, '标题不能超过100个字符']
    },
    content: {
      type: String,
      required: [true, '内容不能为空'],
      trim: true
    },
    summary: {
      type: String,
      trim: true,
      maxlength: [200, '摘要不能超过200个字符']
    },
    category: {
      type: String,
      required: [true, '分类不能为空'],
      enum: ['糖尿病预防', '健康运动', '饮食推荐', '食疗', '健康生活', '医学常识', '用药指南', '并发症预防'],
      default: '健康生活'
    },
    tags: [{
      type: String,
      trim: true
    }],
    imageUrl: {
      type: String,
      default: '' // 配图URL，默认为空
    },
    author: {
      type: String,
      default: '健康管理团队'
    },
    source: {
      type: String,
      default: ''
    },
    viewCount: {
      type: Number,
      default: 0
    },
    likeCount: {
      type: Number,
      default: 0
    },
    isRecommended: {
      type: Boolean,
      default: false
    },
    isPublished: {
      type: Boolean,
      default: true
    },
    publishDate: {
      type: Date,
      default: Date.now
    }
  },
  {
    timestamps: true
  }
);

// 创建索引以支持搜索和分类查询
HealthKnowledgeSchema.index({ title: 'text', content: 'text', category: 1, tags: 1, isRecommended: 1 });

module.exports = mongoose.model('HealthKnowledge', HealthKnowledgeSchema); 